<?php
class BackLinkEngineYahoo extends LogicBackLinkEngine
{
	/**
	 * @param string $url
	 * @return SeoSiteRank
	 */
	public function process($url)
	{
		$url = trim(preg_replace('/https?:\/\//', '', $url)); 
		
		$links = array();
		$yahooUrl = "http://siteexplorer.search.yahoo.com/advsearch?p=http://$url&bwm=i&bwmf=s&bwmo=&fr2=seo-rd-se";
		while($yahooUrl)
		{
			Ezer_Log::debug("Load URL [$yahooUrl]");
			$content = file_get_contents($yahooUrl);
			preg_match_all('/<span class="url">([^<]+)<\/span>/imsU', $content, $matches);
			
			if(count($matches) != 2)
				break;
				
			foreach($matches[1] as $index => $val)
			{
				$backLink = new SeoSiteBackLink();
				$backLink->setEngineType($this->getType());
				$backLink->setSrcUrl($val);
				$links[] = $backLink;
			}
			$page = floor(count($links) / 100) + 1;
			
			$yahooUrl = null;
			if(preg_match("/<a[^>]*href=\"(http:\/\/siteexplorer.search.yahoo.com\/search[^\"]+)\">$page<i/i", $content, $matches))
				$yahooUrl = $matches[1];
		}
		
		Ezer_Log::debug("Found [" . count($links) . "] links");
		return $links;
	}
}